<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ page isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
  String path = request.getContextPath();
  String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
  String _id=request.getParameter("_");
%>
<p id="pSids" style="display: none">${sids}</p>
<div class="jqGrid_wrapper">
  <table id="dialog_group_table"></table>
  <div id="dialog_group_page"></div>
</div>

<script>

    var selectedGroups = [
        <c:forEach var="item" items="${selectedGroups}" varStatus="status">
        {"id":"${item.id}","name":"${item.name}}"} <c:if test="${!status.last}">,</c:if>
        </c:forEach>
    ];

  $(function(){
    // 加载数据
    init_grid_dialog_group();
    // 隐藏表格的全选
    $("#cb_dialog_group_table").hide();
  });

  // 初始化表单数据
  function init_grid_dialog_group() {

    __init_jqgrid('dialog_group_table', 'dialog_group_page',
            '<%=path%>/main/group/list',
            ['名称',   'id'],
            [
                {name: 'name', index: 'name', width: 60, sortable: true, stype: 'text', searchoptions: {sopt: ['cn']}},
                {name: 'id', index: 'id', width: 100, key: true, hidden: true, hidedlg: true}
            ],
            true,
            {
                multiselect: true,
                autowidth: false,
                width: 558,
                onSelectRow: function (rowId, status) {
                    if(status){ // true 选中状态
                        setSelectValue();
                    } else {    // false 取消选中状态
                        deletetSelectValue(rowId);
                    }
                },
                gridComplete: function(){
                    var sids = $("#pSids").text();
                    if(sids){
                        var arr = sids.split(",");
                        for(var i=0; i<arr.length; i++){
                            $(this).jqGrid("setSelection",arr[i]);
                        }
                    }
                }
            }
    )

  }

  /* 设置选中id到p */
  function setSelectValue(){

      var rowId = $("#dialog_group_table").jqGrid('getGridParam', 'selrow'); //获取选中的Id

      var value = "";
      var selectedValue = $("#pSids").text();   /* 获取此前选中值 */
      if(selectedValue != "" && selectedValue != null){
          if(selectedValue.indexOf(rowId) < 0){
              value = selectedValue + "," +　rowId;
          } else {
              return false;
          }
      } else {
          value = rowId;
      }
      /* 将选中的值设置在页面中 */
      $("#pSids").text(value);

      /* 同时将选中的值添加到数组中 */
      var rowDatas = $("#dialog_group_table").jqGrid('getRowData', rowId);
      var addGroup = {"id":rowDatas.id,"name":rowDatas.name};
      selectedGroups.push(addGroup);
  }

  //取消时删除此id
  function deletetSelectValue(rowId){
      for(var i=0; i<selectedGroups.length; i++){
          if(rowId==selectedGroups[i].id){
              selectedGroups.splice(i,1);    // 相等时，删除数组里面对应的对象
              break;
          }
      }
//      console.log("selectedPowers size:"+selectedPowers.length);

      var selectedValue = $("#pSids").text();
      var str = selectedValue.split(",");
      var selectedValueLast = "";
      for(var i = 0; i < str.length; i++){
          if(rowId != str[i]){
              selectedValueLast += str[i] + ",";
          }
      }
      selectedValueLast = selectedValueLast.substring(0, selectedValueLast.length-1);
      /* 将选中的值设置在页面中 */
      $("#pSids").text(selectedValueLast);
  }

  function fn_dialog_group_select(callback) {
      /*
      if(selectedPowers.length < 1){
          __toastr_warning('请至少选择一个职权');
          callback();
          return;
      }*/
      var groupNames = "";
      var groupIds = "";
      for(var i=0; i<selectedGroups.length; i++){
          groupNames += selectedGroups[i].name + ",";
          groupIds += selectedGroups[i].id + ",";
      }
      groupNames = groupNames.substring(0,groupNames.length-1);
      groupIds =  groupIds.substring(0, groupIds.length-1);
      callback({"names":groupNames,"ids":groupIds});
  }

</script>